package com.han.dao.user;

import com.han.dao.BaseDao;
import com.han.pojo.Role;
import com.han.pojo.User;
import com.han.service.user.UserService;
import com.han.service.user.UserServiceImpl;
import com.mysql.jdbc.StringUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserDaoImpl implements UserDao{
    //用户登录，
    public User getUserLogin(Connection connection, String userCode) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        if (connection != null) {
            String sql = "select * from smbms_user where userCode = ?";
            Object[] params = {userCode};
            resultSet = BaseDao.query(connection, preparedStatement, sql, params);
            if (resultSet.next()){
                user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUserCode(resultSet.getString("userCode"));
                user.setUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userPassword"));
                user.setGender(resultSet.getInt("gender"));
                user.setBirthday(resultSet.getDate("birthday"));
                user.setPhone(resultSet.getString("phone"));
                user.setAddress(resultSet.getString("address"));
                user.setUserRole(resultSet.getInt("userRole"));
                user.setCreatedBy(resultSet.getInt("createdBy"));
                user.setCreationDate(resultSet.getTimestamp("creationDate"));
                user.setModifyBy(resultSet.getInt("modifyBy"));
                user.setModifyDate(resultSet.getTimestamp("modifyDate"));
            }
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return user;
    }

    //修改密码
    public int updateUser(Connection connection, int id, String userPassword) throws SQLException {
        PreparedStatement preparedStatement = null;
        Object[] params = {userPassword,id};
        int i = 0;
        if (connection != null){
            String sql = "update smbms_user set userPassword = ? where id = ?";
            i = BaseDao.update(connection, preparedStatement, sql, params);
            BaseDao.closeResource(null,preparedStatement,null);
        }
        return i;
    }

    //查询用户数量
    public int getUserCount(Connection connection, String userName, int userRole) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet =null;
        int count = 0;
        if (connection != null){
            StringBuffer sql = new StringBuffer();
            sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id");
            ArrayList<Object> list = new ArrayList<Object>();
            if (!StringUtils.isNullOrEmpty(userName)){
                sql.append(" and u.userName like ?");
                list.add("%"+userName+"%");
            }
            if (userRole > 0){
                sql.append(" and u.userRole = ?");
                list.add(userRole);
            }
            resultSet = BaseDao.query(connection, preparedStatement, sql.toString(), list.toArray());
            if (resultSet.next()){
                count = resultSet.getInt("count");
            }
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return count;
    }

    //通过条件查询-userList
    public List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws Exception {
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        List<User> userList = new ArrayList<User>();
        if (connection != null){
            List<Object> params = new ArrayList<Object>();
            StringBuffer sql = new StringBuffer();
            sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id");
            if (!StringUtils.isNullOrEmpty(userName)){
                sql.append(" and u.userName like ?");
                params.add("%"+userName+"%");
            }
            if (userRole > 0){
                sql.append(" and u.userRole = ?");
                params.add(userRole);
            }
            //在数据库中，分页显示 limit startIndex，pageSize；总数
            //当前页  (当前页-1)*页面大小
            //0,5	1,0	 01234
            //5,5	5,0	 56789
            //10,5	10,0 10~
            //order by creationDate DESC按creationDate降序排序 asc是升序
            sql.append(" order by creationDate DESC limit ?,?");
            currentPageNo = (currentPageNo-1)*pageSize;
            params.add(currentPageNo);
            params.add(pageSize);
            rs = BaseDao.query(connection, preparedStatement, sql.toString(), params.toArray());
            while(rs.next()){
                User _user = new User();
                _user.setId(rs.getInt("id"));
                _user.setUserCode(rs.getString("userCode"));
                _user.setUserName(rs.getString("userName"));
                _user.setGender(rs.getInt("gender"));
                _user.setBirthday(rs.getDate("birthday"));
                _user.setPhone(rs.getString("phone"));
                _user.setUserRole(rs.getInt("userRole"));
                _user.setUserRoleName(rs.getString("userRoleName"));
                userList.add(_user);
            }
            BaseDao.closeResource(null, preparedStatement, rs);
        }
        return userList;
    }

    public int addUser(Connection connection, List<Object> user) throws SQLException {
        PreparedStatement preparedStatement = null;
        int i = 0;
        if (connection != null){
            String sql = "insert into smbms_user (userCode,userName,userPassword," +
                    "userRole,gender,birthday,phone,address)" +
                    "values(?,?,?,?,?,?,?,?)";
            i = BaseDao.update(connection, preparedStatement, sql, user.toArray());
            BaseDao.closeResource(null,preparedStatement,null);
        }
        return i;
    }

    public boolean deleteUser(Connection connection, int id) throws SQLException {
        PreparedStatement preparedStatement  = null;
        boolean is = false;
        if (connection != null){
            Object[] params = {id};
            String sql = "delete from smbms_user where id = ?";
            int update = BaseDao.update(connection, preparedStatement, sql, params);
            if (update == 1){
                is = true;
            }
            BaseDao.closeResource(null, preparedStatement, null);
        }
        return is;
    }

    public User getUserById(Connection connection, int id) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        if (connection != null){
            Object[] params = {id};
            String sql = "select * from smbms_user where id = ?";
            resultSet = BaseDao.query(connection, preparedStatement, sql, params);
            if (resultSet.next()){
                user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUserCode(resultSet.getString("userCode"));
                user.setUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userPassword"));
                user.setGender(resultSet.getInt("gender"));
                user.setBirthday(resultSet.getDate("birthday"));
                System.out.println(resultSet.getDate("birthday"));
                user.setPhone(resultSet.getString("phone"));
                user.setAddress(resultSet.getString("address"));
                user.setUserRole(resultSet.getInt("userRole"));
                user.setCreatedBy(resultSet.getInt("createdBy"));
                user.setCreationDate(resultSet.getTimestamp("creationDate"));
                user.setModifyBy(resultSet.getInt("modifyBy"));
                user.setModifyDate(resultSet.getTimestamp("modifyDate"));
            }
            System.out.println(user.getBirthday());
            BaseDao.closeResource(null, preparedStatement, resultSet);
        }
        return user;
    }


    public int ModifyUser(Connection connection, User user) throws SQLException {
        PreparedStatement preparedStatement = null;
        int i = 0;
        if (connection != null){
            List<Object> list = new ArrayList<Object>();
            list.add(user.getUserName());
            list.add(user.getGender());
            list.add(user.getBirthday());
            list.add(user.getPhone());
            list.add(user.getAddress());
            list.add(user.getUserRole());
            list.add(user.getModifyBy());
            list.add(new Date());
            list.add(user.getId());
            String sql = "UPDATE smbms_user" +
                    " SET userName = ?, gender = ?, birthday = ?, phone = ?, address = ?, userRole = ?, modifyBy = ?, modifyDate = ?" +
                    " WHERE id = ?";
            i = BaseDao.update(connection, preparedStatement, sql, list.toArray());
            BaseDao.closeResource(null, preparedStatement, null);
        }
        return i;
    }
}
